package com.fullertontech.framework.jdbc;


import java.io.IOException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.fullertontech.framework.global.GlobalUtils;
import com.fullertontech.framework.jdbc.log.SqlLogImpl;
import com.fullertontech.framework.logger.Logger;
import com.github.pagehelper.PageInterceptor;

@Configuration
@Import({DataSourceConfig.class,TransactionManagerConfig.class,MapperScanConfig.class})
public class JdbcConfig {
    
    @Bean
    @Conditional(MybatisPluginCondition.class)
	public org.apache.ibatis.session.Configuration configuration() {
    	org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
		configuration.setMapUnderscoreToCamelCase(true);
		EnableJDBC enableJDBC=GlobalUtils.getAnnotation(EnableJDBC.class);
		if(enableJDBC.showSql()) {
			configuration.setLogImpl(SqlLogImpl.class);
		}
		return configuration;
	}
	/**
	 * 通用mapper模式
	 * @param configuration
	 * @param dataSource
	 * @param interceptor
	 * @return
	 */
	@Bean
	@Conditional(MybatisPluginCondition.class)
	public SqlSessionFactoryBean productSqlSessionFactoryBean(org.apache.ibatis.session.Configuration configuration,
			DataSource dataSource,Interceptor[] interceptor) {
		SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setConfiguration(configuration);
		sqlSessionFactoryBean.setDataSource(dataSource);
		sqlSessionFactoryBean.setPlugins(interceptor);
		try {
			Resource[] resources = new PathMatchingResourcePatternResolver()
			        .getResources("classpath:mapper/*.xml");
			sqlSessionFactoryBean.setMapperLocations(resources);
		} catch (IOException e) {
			Logger.info("没有在src/main/resources/mapper下加载到*Mapper.xml配置文件");
		}
		return sqlSessionFactoryBean;
	}
	/**
	 * 通用mapper
	 * @return
	 */
	@Bean
	public Interceptor[] productInterceptor() {
	    PageInterceptor pageInterceptor=new PageInterceptor();
		Properties p=new Properties();
		p.put("dialect", "mysql");
		pageInterceptor.setProperties(p);
		return new Interceptor[] {pageInterceptor};
	}
	
	
	@Bean
	@Conditional(MybatisPlusPluginCondition.class)
	public MybatisConfiguration mybatisConfiguration() {
		MybatisConfiguration configuration=new MybatisConfiguration();
		configuration.setMapUnderscoreToCamelCase(true);
		EnableJDBC enableJDBC=GlobalUtils.getAnnotation(EnableJDBC.class);
		if(enableJDBC.showSql()) {
			configuration.setLogImpl(SqlLogImpl.class);
		}
		return configuration;
	}
	
	/**
	 * mybatis-plus模式
	 * @param configuration
	 * @param dataSource
	 * @param interceptor
	 * @return
	 */
	@Bean
	@Conditional(MybatisPlusPluginCondition.class)
	public MybatisSqlSessionFactoryBean productMybatisPlusSqlSessionFactoryBean(MybatisConfiguration configuration,
			DataSource dataSource,Interceptor[] interceptor) {
		MybatisSqlSessionFactoryBean sqlSessionFactoryBean=new MybatisSqlSessionFactoryBean();
		sqlSessionFactoryBean.setConfiguration(configuration);
		sqlSessionFactoryBean.setDataSource(dataSource);
		sqlSessionFactoryBean.setPlugins(interceptor);
		sqlSessionFactoryBean.setTypeAliasesPackage(GlobalUtils.getMainClassPackageName().concat(".model"));
		try {
			Resource[] resources = new PathMatchingResourcePatternResolver()
			        .getResources("classpath*:mapper/*.xml");
			sqlSessionFactoryBean.setMapperLocations(resources);
		} catch (IOException e) {
			Logger.info("没有在src/main/resources/mapper下加载到*Mapper.xml配置文件");
		}
		return sqlSessionFactoryBean;
	}
}
